<?php
namespace Home\Controller;
use Think\Controller;
class IndexController extends CommonController {

	public function _initialize(){
		parent::_initialize();
	}

	public function index(){
		//郑重推荐
		$Sort = M("Sort");
		$Sortlist = $Sort->order("id ASC")->limit(3)->select();
		$Goods = M("Goods");
		$Review =M("Review");
		$featuredgoods = array();
		foreach ($Sortlist as $key=>$value) {
			//解决模板问题
			switch ($key) {
				case 0:
					$value['data-category-id'] = 20;
					break;
				case 1:
					$value['data-category-id'] = 18;
					break;
				case 2:
					$value['data-category-id'] = 25;
					break;
			}
			$goodsdata = $Goods->where("sortid ={$value['id']} ")->limit(5)->select();
			if (!empty($goodsdata)) {
				$goodslist = array();
				foreach ($goodsdata as $key => $val) {
					$val['goodsimg'] = json_decode($val['goodsimg'],true);
					$ratingsum = 0;
					$reviewcount = $Review->where("goodsid ={$val['id']}")->count();
					if ($reviewcount==0) {
						$val['ratingaverage'] = 0;
					}else{
						$reviewlist = $Review->where("goodsid ={$value['id']}")->select();
						foreach ($reviewlist as $vals) {
							$ratingsum = $vals['rating']+$ratingsum;
						}
						$val['ratingaverage'] = round($ratingsum/$reviewcount);
					}
					$goodslist[ ] = $val;
				}
				$value['goodslit'] = $goodslist;
			}
			$featuredgoods[ ] =  $value;
		}
		if (!empty($_SESSION['USERID'])) {
			$this->getshopcart();
			$this->getcolmsg();
		}
		//$this->loadGoodsul();
		$this->loadnewgoods();
		$this->loadlunbo();
		$this->assign("featuredgoods",$featuredgoods);
		$this->display("index");   
	}
	//载入新上架商品数据
	public function loadnewgoods(){
		$Sort = M("Sort");
		$Sortlist = $Sort->order("id ASC")->limit(3)->select();
		$Goods = M("Goods");
		$Review =M("Review");
		$newgoods = array();
		foreach ($Sortlist as $key=>$value) {
			//解决模板问题
			switch ($key) {
				case 0:
					$value['data-category-id'] = 1;
					break;
				case 1:
					$value['data-category-id'] = 2;
					break;
				case 2:
					$value['data-category-id'] = 3;
					break;
			}
			$goodsdata = $Goods->where("sortid ={$value['id']} ")->limit(5)->select();
			if (!empty($goodsdata)) {
				$goodslist = array();
				foreach ($goodsdata as $key => $val) {
					$val['goodsimg'] = json_decode($val['goodsimg'],true);
					$ratingsum = 0;
					$reviewcount = $Review->where("goodsid ={$val['id']}")->count();
					if ($reviewcount==0) {
						$val['ratingaverage'] = 0;
					}else{
						$reviewlist = $Review->where("goodsid ={$value['id']}")->select();
						foreach ($reviewlist as $vals) {
							$ratingsum = $vals['rating']+$ratingsum;
						}
						$val['ratingaverage'] = round($ratingsum/$reviewcount);
					}
					$goodslist[ ] = $val;
				}
				$value['goodslit'] = $goodslist;
			}
			$newgoods[ ] =  $value;
		}
		$this->assign("newgoods",$newgoods);
	}
	//首页轮播
	public function loadlunbo(){
		$Goods = M("Goods");
		$lunbogoods = $Goods->order("id desc")->limit(3)->select();
		$lunbolist = array();
		foreach ($lunbogoods as $value) {
			$value['goodsimg'] = json_decode($value['goodsimg'],true);
			$lunbolist[] = $value;
		}
		$this->assign("lunbolist",$lunbolist);

	}
	
}